package com.lanchetech.admin.service.impl;


import com.alibaba.excel.util.CollectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.admin.service.CoinService;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.*;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.CoinProductVO;
import com.lanchetech.common.enums.DeletedEnum;
import com.lanchetech.common.enums.LogisticsStatusEnum;
import com.lanchetech.dao.*;
import com.lanchetech.entity.CoinOrder;
import com.lanchetech.entity.CoinProduct;
import com.lanchetech.entity.Spu;
import com.lanchetech.entity.User;
import com.lanchetech.service.DaoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CoinServiceImpl implements CoinService {

    @Autowired
    CoinProductMapper coinProductMapper;

    @Autowired
    SpuMapper spuMapper;

    @Autowired
    DaoService daoService;

    @Autowired
    CoinOrderMapper coinOrderMapper;

    @Autowired
    CoinOrderAddressMapper coinOrderAddressMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public BaseResp editCoinProduct(CoinProduct coinProduct) {
        Spu spu = spuMapper.selectByPrimaryKey(coinProduct.getSpuId());
        coinProduct.setProductName(spu.getName());
        if (coinProduct.getId() == null) {
            coinProduct.setCreatedAt(new Date());
            coinProduct.setUpdatedAt(new Date());
            coinProduct.setSales(0);
            coinProduct.setDeleted(DeletedEnum.DEFAULT.getStatus());
            coinProductMapper.insert(coinProduct);
        } else {
            coinProduct.setUpdatedAt(new Date());
            coinProductMapper.updateByPrimaryKeySelective(coinProduct);
        }
        return new BaseResp();
    }

    @Override
    public ResultData<CoinProductVO> getCoinProductDetail(Long id) {
        CoinProductVO vo = new CoinProductVO();
        CoinProduct coinProduct = coinProductMapper.selectByPrimaryKey(id);
        vo.setCoinProduct(coinProduct);
        vo.setSpu(spuMapper.selectByPrimaryKey(coinProduct.getSpuId()));
        return new ResultData<>(vo);
    }

    @Override
    public ResultData<BasePageResp<CoinProductResp>> getCoinProductPage(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<CoinProductResp> list = coinProductMapper.findAllPage(req);
        if(!CollectionUtils.isEmpty(list)){
            Map<Long, Spu> map = daoService.getSpuMap(list.stream().map(CoinProduct::getSpuId).collect(Collectors.toList()));
            list.stream().forEach(item->{
                item.setMainImage(map.get(item.getSpuId()).getMainImage());
                item.setPrice(map.get(item.getSpuId()).getMinPrice());
                item.setIsVirtual(map.get(item.getSpuId()).getIsVirtual());
            });
        }
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public BaseResp deleteCoinProduct(Long id) {
        coinProductMapper.updateDeletedById(DeletedEnum.DELETED.getStatus(), id);
        return new BaseResp();
    }

    @Override
    public ResultData<BasePageResp<CoinOrderResp>> getCoinOrderPage(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<CoinOrderResp> list = coinOrderMapper.findAllPage(req);
        if(!CollectionUtils.isEmpty(list)){
            Map<Long, Spu> map = daoService.getSpuMap(list.stream().map(CoinOrderResp::getSpuId).collect(Collectors.toList()));
            Map<Long, User> userMap = daoService.getUserMap(list.stream().map(CoinOrder::getUserId).collect(Collectors.toList()));
            list.stream().forEach(item->{
                item.setMainImage(map.get(item.getSpuId()).getMainImage());
                item.setProductName(map.get(item.getSpuId()).getName());
                item.setPrice(map.get(item.getSpuId()).getMinPrice());
                item.setIsVirtual(map.get(item.getSpuId()).getIsVirtual());
                item.setNickname(userMap.get(item.getUserId()).getNickname());
            });
        }
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public ResultData<CoinOrderResp> getCoinOrderDetail(Long id) {
        CoinOrderResp resp = new CoinOrderResp();
        CoinOrder coinOrder = coinOrderMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(coinOrder, resp);
        Spu spu = spuMapper.selectByPrimaryKey(coinOrder.getSpuId());

        resp.setMainImage(spu.getMainImage());
        resp.setProductName(spu.getName());
        resp.setPrice(spu.getMinPrice());
        resp.setIsVirtual(spu.getIsVirtual());

        if(coinOrder.getUserAddressId() != null){
            resp.setCoinOrderAddress(coinOrderAddressMapper.findOneByCoinOrderId(id));
        }else{
            User user = userMapper.selectByPrimaryKey(coinOrder.getUserId());
            resp.setNickname(user.getNickname());
            resp.setPhone(user.getUsername());
        }
        return new ResultData<>(resp);
    }

    @Override
    public BaseResp editCoinOrderLogistics(CoinOrder req) {
        CoinOrder record = coinOrderMapper.selectByPrimaryKey(req.getId());
        record.setLogisticsCompany(req.getLogisticsCompany());
        record.setLogisticsNumber(req.getLogisticsNumber());
        record.setLogisticsStatus(LogisticsStatusEnum.DONE.getStatus());
        record.setDeliveryTime(new Date());
        coinOrderMapper.updateByPrimaryKeySelective(record);
        return new BaseResp();
    }
}